package com.youkeda.app.service.impl;

import com.youkeda.app.dao.CategoryDAO;
import com.youkeda.app.dataobject.CategoryDO;
import com.youkeda.app.model.Category;
import com.youkeda.app.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName CategoryServiceImpl
 * @Author 刘正星
 * @Date 2020/8/20 16:51
 **/
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDAO categoryDAO;

    @Override
    public Category add(Category category) {
        if (category == null) {
            return null;
        }
        CategoryDO categoryDO = new CategoryDO(category);
        int insertSize = categoryDAO.insert(categoryDO);
        if (insertSize < 1) {
            return null;
        }
        category.setId(categoryDO.getId());
        return category;
    }

   /* @Override
    public List<Category> queryAll() {
        List<CategoryDO> categoryDOList = categoryDAO.selectAll();
        if (CollectionUtils.isEmpty(categoryDOList)){
            return Collections.emptyList();
        }
        List<Category> categoryList = categoryDOList.stream().map(categoryDO ->categoryDO.convertToModel()).collect(Collectors.toList());
        Map<String,Category> categoryMap = new HashMap<>();
        //初始化一个虚拟根节点，0 可以对应的是所有一级目录的父亲
        categoryMap.put("0",new Category());
       categoryList.forEach(category -> categoryMap.put(category.getId(),category));
       categoryList.forEach(category -> {
           Category parent = categoryMap.get(category.getParentCategoryId());
           if (parent != null){
               if (parent.getSubCategoryList() == null){
                   parent.setSubCategoryList(new ArrayList<>());
               }
               parent.getSubCategoryList().add(category);
           }
       });


        return categoryList;
    }*/

    public List<Category> queryAll() {
        List<Category> categoryList = new ArrayList<>();
        List<CategoryDO> categoryDOS = categoryDAO.selectAll();
        if (CollectionUtils.isEmpty(categoryDOS)) {
            return Collections.emptyList();
        }
        for (CategoryDO categoryDO : categoryDOS) {
            Category category = categoryDO.convertToModel();
            categoryList.add(category);
        }
        List<String> parentIds = categoryList.stream().map(Category::getId).collect(Collectors.toList());
        List<CategoryDO> categoryDOList = categoryDAO.selectByParentIds(parentIds);
        if (CollectionUtils.isEmpty(categoryDOList)) {
            return categoryList;
        }
        Map<String, List<CategoryDO>> categoryMap = categoryDOList.stream().collect(
                Collectors.groupingBy(CategoryDO::getParentCategoryId));
        for (Category category : categoryList) {
            List<Category> subCategorys = new ArrayList<>();
            List<CategoryDO> subCategoryDOList = categoryMap.get(category.getId());
            if (!CollectionUtils.isEmpty(subCategoryDOList)) {
                for (CategoryDO categoryDO : subCategoryDOList) {
                    Category subCategory = categoryDO.convertToModel();
                    subCategorys.add(subCategory);
                }
                category.setSubCategoryList(subCategorys);
            }
        }
        return categoryList;
    }

    @Override
    public Category get(String id) {
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        CategoryDO categoryDO = categoryDAO.get(id);
        System.out.println(categoryDO.toString());
        if (categoryDO == null) {
            return null;
        }
        String parentid = categoryDO.getId();
        List<CategoryDO> subCategoryDOList = categoryDAO.selectByParentId(parentid);
        List<Category> subCategoryList = subCategoryDOList.stream().map(categoryDO1 -> categoryDO1.convertToModel()).collect(Collectors.toList());
        Category category = categoryDO.convertToModel();
        category.setSubCategoryList(subCategoryList);
        return category;
    }
}
